importPackage(Packages.aurelienribon.tweenengine);
importPackage(Packages.aurelienribon.tweenengine.equations);

var Timeline=Packages.aurelienribon.tweenengine.Timeline;
var Tween=Packages.aurelienribon.tweenengine.Tween;
var Linear=Packages.aurelienribon.tweenengine.equations.Linear.INOUT;

var scheduler=new Object();
scheduler.getSize=function(stage){
	return 1;
}

scheduler.getTimes=function(stage){
	return [0];
}

scheduler.getParameters=function(stage){
	return new Array();
}

scheduler.getMethods=function(stage){
	return ["step1"];
}

scheduler.step1=function(stage){
	
	var sender=stage.getSourcePosition();
	
	var createJump=function(t,x,y){
		return Tween.to(t,1,3).target(x,y).waypoint(Math.random()*500+500,300).ease(Linear);
	}
	var derivate=function(t,x0,x1,y1,wp1,wp2){
		var segment=(x1-x0)/3;
		
		return Tween.to(t,1,3).target(x1,y1);//.waypoint(x0+segment,wp1).waypoint(x0+2*segment,wp2);
	}
	
	var createTimeline=function(t,x0,y0,x1,y1,y2,y3){
		var timeline=Timeline.createSequence().beginSequence();
		timeline.push(createJump(t,x0,y0));
		timeline.push(derivate(t,x0,x1,y1,y2,y3));
		timeline.end();	
		
		return timeline;
	}
	
	for(var i=0;i<stage.getMissileCount();i++){
		
		var actor=stage.createSpecialMissileActor();
		actor.setMissileType(1);
		actor.setModelPosition(sender.x,sender.y);

		var x0=Math.random()*800+100;
		var y0=Math.random()*200+50+stage.getTrackOffset();
		var y1=Math.random()*200+50+stage.getTrackOffset();
		var y2=Math.random()*200+50+stage.getTrackOffset();
		var y3=Math.random()*200+50+stage.getTrackOffset();
		var x1;
		if(x0<500){
			x1=1100;
		} else {
			x1=-100;
		}
		
		stage.tweenSpecialMissileActor(actor,createTimeline(actor,x0,y0,x1,y1,y2,y3));
		
		// create virtual actor for prediction
		var virtualActor=stage.createVirtualSpecialMissileActor(actor);
		virtualActor.setPosition(sender.x,sender.y);
		stage.tweenVirtualSpecialMissileActor(virtualActor,createTimeline(virtualActor,x0,y0,x1,y1,y2,y3));
	}
	
	
}
